#include "json.hpp"
#include <iostream>
#include <fstream>
#include <memory>

using namespace std;


void print(ostream &os) {
    os << "\n";
}
template<typename T, typename... Args>
void print(ostream &os, T &&t, Args&&... args) {
    os << t << " ";
    print(os, std::forward<Args>(args)...);
}

template<typename Iter>
void printArr(ostream &os, Iter begin, Iter end) {
    while (begin != end) {
        os << *begin++ << " ";
    }
    os << "\n";
}

int main(int argc, char *argv[]) {
    using json = nlohmann::json;
    for (int i = 1; i < argc; ++i) {
        string path(argv[i]);
        ifstream ifs(path);
        if (!ifs.is_open()) {
            cerr << path << " open failed" << endl;
            exit(1);
        }
        //convert 2 .txt
        string newPath = path.substr(0, path.find_last_of('.') + 1).append("txt");
        ofstream ofs(newPath);
        if (!ofs.is_open()) {
            cerr << newPath << "open failed" << endl;
            exit(1);
        }
        json data = json::parse(ifs);
        for (auto &&info : data["terminal"]) {
            print(ofs, info["id"], info["port"], string(info["ip"]));
        }
        for (auto &&arr : data["active_connect"]) {
            printArr(ofs, arr.begin(), arr.end());
        }
        print(ofs, data["controller_id"]);
    }
    return 0;
}
